package com.wanmait.wanmarecruit.util;

import org.springframework.util.DigestUtils;

import java.util.Random;

public class PasswordUtils {
    public static String md5(String str){
        return DigestUtils.md5DigestAsHex(DigestUtils.md5DigestAsHex(str.getBytes()).substring(6,29).getBytes());
    }

    public static boolean checkPassword(String originPassword,String md5Password){

        String md5OriginPassword = md5(originPassword);
        return md5OriginPassword.equals(md5Password);
    }

    public static String randomString(int length){
        String chars="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(chars.length());
            builder.append(chars.charAt(randomIndex));
        }
        return builder.toString();
    }

    public static void main(String[] args) {
        System.out.println(PasswordUtils.randomString(5));
    }
}
